import DayNewlyAddLine from '@/pages/statistic/DayNewlyAddLine';
import { dayNewlyAddedAnnouncementUsingPost } from '@/services/backend/announcementController';
import { dayNewlyAddedFileUsingPost } from '@/services/backend/fileInfoController';
import { dayNewlyAddedUserUsingPost } from '@/services/backend/userController';
import { dayNewlyAddedUserSignUsingPost } from '@/services/backend/userSignController';
import { PageContainer } from '@ant-design/pro-components';
import { Col, notification, Row } from 'antd';
import React, { useEffect, useState } from 'react';

const Statistic: React.FC = () => {
  //添加信息
  const [userData, setUserData] = useState<DayNewlyAdd[]>([]);
  const [announcementData, setAnnouncementData] = useState<DayNewlyAdd[]>([]);
  const [fileData, setFileData] = useState<DayNewlyAdd[]>([]);
  const [signData, setSignData] = useState<DayNewlyAdd[]>([]);

  const loadSignStatistics = async () => {
    try {
      const { data, code } = await dayNewlyAddedUserSignUsingPost();
      if (code === 0 && data) {
        const newData = data.map((item) => {
          return {
            ...item,
            num: Number(item.num),
          } as DayNewlyAdd;
        });
        setSignData(newData);
      }
    } catch (e: any) {
      notification.error({
        message: '系统异常',
        description: e.message,
      });
    }
  };

  const loadFileStatistics = async () => {
    try {
      const { data, code } = await dayNewlyAddedFileUsingPost();
      if (code === 0 && data) {
        const newData = data.map((item) => {
          return {
            ...item,
            num: Number(item.num),
          } as DayNewlyAdd;
        });
        setFileData(newData);
      }
    } catch (e: any) {
      notification.error({
        message: '系统异常',
        description: e.message,
      });
    }
  };

  const loadAnnouncementStatistics = async () => {
    try {
      const { data, code } = await dayNewlyAddedAnnouncementUsingPost();
      if (code === 0 && data) {
        const newData = data.map((item) => {
          return {
            ...item,
            num: Number(item.num),
          } as DayNewlyAdd;
        });
        setAnnouncementData(newData);
      }
    } catch (e: any) {
      notification.error({
        message: '系统异常',
        description: e.message,
      });
    }
  };
  const loadUserStatistics = async () => {
    try {
      const { data, code } = await dayNewlyAddedUserUsingPost();
      if (code === 0 && data) {
        const newData = data.map((item) => {
          return {
            ...item,
            num: Number(item.num),
          } as DayNewlyAdd;
        });
        setUserData(newData);
      }
    } catch (e: any) {
      notification.error({
        message: '系统异常',
        description: e.message,
      });
    }
  };

  //页面加载时的钩子
  useEffect(() => {
    //加载数据
    loadUserStatistics();
    loadAnnouncementStatistics();
    loadFileStatistics();
    loadSignStatistics();
  }, []);

  return (
    <PageContainer>
      <Row gutter={24}>
        <Col
          span={12}
          style={{
            marginBottom: 24,
          }}
        >
          <DayNewlyAddLine title={'近30日新增用户统计'} data={userData} />
        </Col>
        <Col
          span={12}
          style={{
            marginBottom: 24,
          }}
        >
          <DayNewlyAddLine title={'近30日新增公告统计'} data={announcementData} />
        </Col>
      </Row>
      <Row gutter={24}>
        <Col
          span={12}
          style={{
            marginBottom: 24,
          }}
        >
          <DayNewlyAddLine title={'近30日新增文件统计'} data={fileData} />
        </Col>
        <Col
          span={12}
          style={{
            marginBottom: 24,
          }}
        >
          <DayNewlyAddLine title={'近30日签到人数统计'} data={signData} />
        </Col>
      </Row>
    </PageContainer>
  );
};

export default Statistic;
